home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / freeWAIS-sf-1.1 / ui / waisq.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-04  |  10.1 KB  |  435 lines

  1. /* 
  2.   WIDE AREA INFORMATION SERVER SOFTWARE:
  3.    No guarantees or restrictions.  See the readme file for the full standard
  4.    disclaimer.
  5.  
  6.    This is part of the shell user-interface tools for the WAIS software.
  7.    Do with it as you please.
  8.  
  9.    jonathan@Think.COM
  10. */
  11.  
  12. /* Copyright (c) CNIDR (see ../COPYRIGHT) */
  13.  
  14.  
  15. /* $Log: waisq.c,v $
  16.  * Revision 1.2  1994/08/05  07:20:20  pfeifer
  17.  * Release beta 04
  18.  *
  19.  * Revision 1.1  93/06/23  20:02:24  warnock
  20.  * Initial revision
  21.  * 
  22.  * Revision 1.1  1993/02/16  15:09:27  freewais
  23.  * Initial revision
  24.  *
  25.  * Revision 1.25  92/06/03  17:30:33  jonathan
  26.  * Removed declarations for waislogfile and wais_log_file_name (in ir/cutil.h now)
  27.  * 
  28.  * Revision 1.24  92/04/29  13:20:27  jonathan
  29.  * Updated for release.
  30.  * 
  31.  * Revision 1.23  92/04/01  17:21:27  jonathan
  32.  * changed loadsource to findsource (which now loads if necessary).
  33.  * 
  34.  * Revision 1.22  92/03/17  14:36:06  jonathan
  35.  * Modified to use new ui functions in wais-ui.  Generally cleaned up.
  36.  * 
  37.  * Revision 1.21  92/03/06  14:52:02  jonathan
  38.  * New and Improved source loading!
  39.  * 
  40.  * Revision 1.20  92/02/29  19:40:10  jonathan
  41.  * Only read source for waisping (not all sources).  Much faster.
  42.  * 
  43.  * Revision 1.19  92/02/15  19:47:52  jonathan
  44.  * Added $Log for RCS
  45.  * 
  46. */
  47.  
  48. #include "wais.h"
  49.  
  50. #define MAIN
  51. #include "globals.h"
  52.  
  53. #define WAISQ_DATE "Thu Aug 27 1992"
  54.  
  55. /* this will take as its argument the name of a question file,
  56.    and will do all the right things with it.
  57. */
  58.  
  59. #ifdef ANSI_LIKE /* use ansi varargs */
  60. void PrintStatus(long logtype, long loglevel, char* format, ...)
  61. #else /* k&r */
  62. void PrintStatus(va_alist)
  63. va_dcl
  64. #endif /* ANSI_LIKE */
  65. {
  66.   va_list ap;            /* the variable arguments */
  67.   char message[1000];
  68.  
  69. #ifndef ANSI_LIKE
  70.   long logtype, loglevel;
  71.   char *format;
  72.  
  73.   va_start(ap);
  74.   logtype = va_arg(ap, int);
  75.   loglevel = va_arg(ap, int);
  76.   format = va_arg(ap, char*);
  77. #else
  78.   va_start(ap, format);
  79. #endif
  80.  
  81.   vsprintf(message, format, ap);
  82.   fprintf(stderr, "%s", message);
  83. #ifndef ANSI_LIKE
  84.   va_end(ap);
  85. #endif
  86. }
  87.  
  88. /* Dummy function added so we can link to wais-ui.c.  swais uses the
  89.  * real version of this routine, which is in swais.c, but it's called
  90.  * in here -- warnock@hypatia.gsfc.nasa.gov, 7/21/93
  91.  */
  92. void
  93. DumpTextToScreen(buffer, size)
  94. char *buffer;
  95. long size;
  96. {
  97. }   
  98.  
  99. usage(name) 
  100. char *name;
  101. {
  102.   if (strstr(name, "waisq") != NULL) {
  103.     fprintf(stderr,"Usage: waisq\n");
  104.     fprintf(stderr,"  [-f question_file] /* defaults to stdin, stdout */\n");
  105.     fprintf(stderr,"  [-s sourcedir] /* or environment variable WAISSOURCEDIR, or ~/wais-sources/ */\n");
  106.     fprintf(stderr,"  [-S sourcename]\n");
  107.     fprintf(stderr,"  [-c common_sourcedir] /* or environment variable WAISCOMMONSOURCEDIR */\n");
  108.     fprintf(stderr,"  [-m max_Result_Docs] /* defaults to 40 */\n");
  109.     fprintf(stderr,"  [-v document_number]\n");
  110.     fprintf(stderr,"  [-g]    /* to do a search */\n");
  111.     fprintf(stderr,"  [-h]    /* this message */\n");
  112.     fprintf(stderr,"  [-t]    /* test a source */\n");
  113.     fprintf(stderr,"  [-V]    /* print version */\n");
  114.     fprintf(stderr,"  [word [word [...]]]\n");
  115.     fprintf(stderr,"or %s -    /* interactive mode */\n", name);
  116.   }
  117.   else {
  118.     fprintf(stderr,"Usage: waisping\n");
  119.     fprintf(stderr,"  [-s sourcedir] /* or environment variable WAISSOURCEDIR, or ~/wais-sources/ */\n");
  120.     fprintf(stderr,"  [-c common_sourcedir] /* or environment variable WAISCOMMONSOURCEDIR */\n");
  121.     fprintf(stderr,"  sourcename\n");
  122.   }
  123. }
  124.  
  125. void SetSource(question, sourcename)
  126. Question question;
  127. char *sourcename;
  128. {
  129.   SourceList s;
  130.   SourceID sid;
  131.  
  132.   sid = (SourceID)s_malloc(sizeof(_SourceID));
  133.   s = (SourceList)s_malloc(sizeof(_SourceList));
  134.   s->thisSource = sid;
  135.   s->nextSource = NULL;
  136.   sid->filename = sourcename;
  137.   question->Sources = s;
  138. }
  139.  
  140. char *
  141. fixdirname(dir)
  142. char *dir;
  143. {
  144.   char *res;
  145.  
  146.   if(dir[strlen(dir)-1] == '/') res = dir;
  147.   else {
  148.     res = s_malloc(strlen(dir)+2);
  149.     sprintf(res,"%s/", dir);
  150.   }
  151.   return res;
  152. }
  153.  
  154. main(argc, argv)
  155.      int argc;
  156.      char *argv[];
  157. {
  158.   Question question;
  159.   char msg[STRINGSIZE];
  160.   char *qfilename, *sourcename;
  161.   char keywords[STRINGSIZE];
  162.   int i, *document_number, numdocs, maxDocs;
  163.   Boolean dosearch, interactive_mode, test_mode, rescan;
  164.   FILE *fp;
  165.   char *cdir, *sdir;
  166. #ifndef SABER
  167.   char *getenv();
  168. #endif
  169.  
  170.   if (command_name = (char*)rindex(argv[0], '/'))
  171.     command_name++;
  172.   else
  173.     command_name = argv[0];
  174.  
  175.   sdir = cdir = qfilename = sourcename = NULL;
  176.   keywords[0] = 0;
  177.   dosearch = interactive_mode = test_mode = FALSE;
  178.  
  179.   NumSources = 0;
  180.   maxDocs = 40;
  181.  
  182.   document_number = (int*)s_malloc(sizeof(int)*(maxDocs));
  183.   numdocs = 0;
  184.  
  185.   if(strstr(argv[0], "waisping") != NULL) test_mode = TRUE;
  186.  
  187.   /* parse arguments */
  188.  
  189.   if (argc == 1) {
  190.     usage(argv[0]);
  191.     exit(0);
  192.   }
  193.  
  194.   /* if the first arg is '-', do interactive mode */
  195.  
  196.   i = 1;
  197.  
  198.   if((argv[1][0] == '-') && (argv[1][1] == 0)) {
  199.     interactive_mode = TRUE;
  200.     i++;
  201.   }
  202.   for(; i < argc; i++) {
  203.     if (*argv[i] == '-') {
  204.       argv[i]++;
  205.       switch (*argv[i]) {
  206.       case 'f':
  207.     i++;
  208.     if(i >= argc) {
  209.       fprintf(stderr, "Too few arguments: file name missing.\n");
  210.       exit(1);
  211.       }
  212.     qfilename = argv[i];
  213.     break;
  214.       case 'c':
  215.     i++;
  216.     if(i >= argc) {
  217.       fprintf(stderr, "Too few arguments: source directory missing.\n");
  218.       exit(1);
  219.       }
  220.     cdir = argv[i];
  221.     break;
  222.       case 's':
  223.     i++;
  224.     if(i >= argc) {
  225.       fprintf(stderr, "Too few arguments: source directory missing.\n");
  226.       exit(1);
  227.       }
  228.     sdir = argv[i];
  229.     break;
  230.       case 'S':
  231.     i++;
  232.     if(i >= argc) {
  233.       fprintf(stderr, "Too few arguments: source name missing.\n");
  234.       exit(1);
  235.       }
  236.     sourcename = argv[i];
  237.     break;
  238.       case 'm':
  239.     i++;
  240.     if(i >= argc) {
  241.       fprintf(stderr, "Too few arguments: source name missing.\n");
  242.       exit(1);
  243.       }
  244.     maxDocs = atoi(argv[i]);
  245.     document_number = (int*)s_realloc(document_number,
  246.                      sizeof(int)*maxDocs);
  247.     break;
  248.       case 'v':
  249.     i++;
  250.     if(i >= argc) {
  251.       fprintf(stderr, "Too few arguments: Document number missing.\n");
  252.       exit(1);
  253.       }
  254.     document_number[numdocs++] = atoi(argv[i]);
  255.     numdocs = MINIMUM(maxDocs, numdocs);
  256.     break;
  257.       case 'g':
  258.     dosearch = TRUE;
  259.     break;
  260.       case 'h':
  261.     usage(argv[0]);
  262.     exit(0);
  263.     break;
  264.       case 't':
  265.     test_mode = TRUE;
  266.     break;
  267.       case 'V':
  268.     printf("%s version: %s, %s.\n", argv[0], VERSION, WAISQ_DATE);
  269.     exit(0);
  270.       default:
  271.     fprintf(stderr, "Unknown option: %s.\n", argv[i]);
  272.     exit(1);
  273.       }
  274.     }
  275.     else {
  276.       if(test_mode)
  277.     strcpy(keywords, argv[i]);
  278.       else if((strlen(keywords) + strlen(argv[i]) + 1) < STRINGSIZE) {
  279.     strcat(keywords, argv[i]);
  280.     strcat(keywords, " ");
  281.       }
  282.     }
  283.   }
  284.       
  285.   if(sdir == NULL) {
  286.     if((sdir = getenv("WAISSOURCEDIR")) == NULL) {
  287.       sprintf(msg, "%s/wais-sources/", getenv("HOME"));
  288.       sdir = s_strdup(msg);
  289.     }
  290.   }
  291.  
  292.   if(cdir == NULL) cdir = getenv("WAISCOMMONSOURCEDIR");
  293.  
  294.   sdir = fixdirname(sdir);
  295.  
  296.   if(cdir != NULL) {
  297.     cdir = fixdirname(cdir);
  298.   }
  299.  
  300.   the_Question = (Question)s_malloc(sizeof(_Question));
  301.  
  302.   the_Question->maxresdocs = maxDocs;
  303.  
  304.   the_Question->sourcepath = s_malloc((sdir?strlen(sdir):0)+(cdir?strlen(cdir):0)+2);
  305.   sprintf(the_Question->sourcepath, "%s:%s", (sdir?sdir:""), (cdir?cdir:""));
  306.  
  307.   if(test_mode) {
  308.  
  309.     question = the_Question;
  310.  
  311.     SetSource(question, keywords);
  312.     if(findsource(keywords, question->sourcepath) == NULL) {
  313.       PrintStatus(STATUS_URGENT, STATUS_HIGH,
  314.           "Can't find source: %s\n", keywords);
  315.     }
  316.     else
  317.       test_connection(question);
  318.     exit(0);
  319.   }
  320.  
  321.   else if(!interactive_mode) {
  322.     if (qfilename != NULL) {
  323.       if(strcmp(qfilename, "-")) {
  324.     if((fp = fopen(qfilename, "r")) == NULL) {
  325.       fprintf(stderr, "Can't open question %s\n", qfilename);
  326.       exit(-1);
  327.     }
  328.     ReadQuestion(the_Question, fp);
  329.     fclose(fp);
  330.       }
  331.     }
  332.     else {
  333.       fp = stdin;
  334.       ReadQuestion(the_Question, fp);
  335.     }
  336.  
  337.     question = the_Question;
  338.  
  339.     question->modified = TRUE;
  340.  
  341.     question->numsources =
  342.       listlength((List)question->Sources);
  343.  
  344.     question->numdocs =
  345.       listlength((List)question->RelevantDocuments);
  346.  
  347.     question->numresdocs =
  348.       listlength((List)question->ResultDocuments);
  349.  
  350.     if (keywords[0] != 0) {
  351.       strcpy(question->keywords,keywords);
  352.     }
  353.  
  354.     if (sourcename != NULL) {
  355.       SetSource(question, sourcename);
  356.     }
  357.  
  358.     if(dosearch == TRUE) {
  359.       SearchWais(question);
  360.       if((qfilename == NULL) || (strcmp(qfilename, "-") == 0))
  361.     WriteQuestionfp(stdout, question);
  362.       else
  363.     WriteQuestion(qfilename, question);
  364.     }
  365.     else if((qfilename != NULL) &&
  366.         strcmp(qfilename, "-") == 0)
  367.       WriteQuestionfp(stdout, question);
  368.  
  369.     for(i = 0; i < numdocs; i++) {
  370.       if((document_number[i] > 0) &&
  371.      (document_number[i] <= question->numresdocs)) { /* view a document */
  372.     DocumentID doc;
  373.     char *viewbuffer;
  374.     long size = 0;
  375.  
  376.     if((doc = findDoc(question->ResultDocuments, document_number[i]-1))
  377.        == NULL) {
  378.       PrintStatus(STATUS_URGENT, STATUS_HIGH,
  379.               "Unable to find document.  This should not happen.\n");
  380.       return;
  381.     }
  382.     PrintStatus(STATUS_INFO, STATUS_LOW, 
  383.             "Viewing Document %d: %s\n", document_number[i], doc->doc->headline);
  384.     GetWaisDocument(question, doc, 
  385.             doc->doc->type[0], stdout, &size);
  386.       }
  387.     }
  388.   }
  389.   else {
  390.     /* interactive mode!
  391.        read questions from standard in, write to stdout, until an empty question comes through.
  392.        */
  393.     question = the_Question;
  394.     while(interactive_mode) {
  395.       if(ReadQuestion(question, stdin)) {
  396.     question->modified = TRUE;
  397.     question->numsources =
  398.       listlength((List)question->Sources);
  399.     question->numdocs =
  400.       listlength((List)question->RelevantDocuments);
  401.     question->numresdocs =
  402.       listlength((List)question->ResultDocuments);
  403.  
  404.     /* cases */
  405.     if((strlen(question->keywords) != 0) ||
  406.        (question->numdocs > 0)) {
  407.       SearchWais(question);
  408.       WriteQuestionfp(stdout, question);
  409.     }
  410.     else if(question->numresdocs > 0)
  411.       for(i = 0; i < question->numresdocs; i++) {
  412.         char* viewbuffer;
  413.         long size = 0;
  414.         DocumentID doc;
  415.  
  416.         doc = findDoc(question->ResultDocuments, i);
  417.         if(doc != NULL) {
  418.           GetWaisDocument(question, doc, doc->doc->type[0],
  419.                   stdout, &size);
  420.         }
  421.       }
  422.     else {
  423.       SearchWais(question);
  424.       WriteQuestionfp(stdout, question);
  425.     }
  426.     fflush(stdout);
  427.     PrintStatus(STATUS_INFO, STATUS_LOW,
  428.             "Waisq: Ready for next question.\n");
  429.       }
  430.       else interactive_mode = FALSE;
  431.     }
  432.   }
  433.   exit(0);
  434. }
  435.